<?php 

abstract class Engine_Form_Element {

    protected $name;
    protected $label;
	protected $value;
    protected $help;
    protected $isRequire;
    protected $errors = array();
    protected $attribs = array();
    protected $validators = array();
    public $showLabel = false;

    abstract public function compile();

    abstract public function populate($value);

    public function __construct($name) {
        $this->setName($name);
    }

    public function getName() {
        return $this->name;
    }

    public function setName($value) {
        $this->name = $value;
        $this->setAttrib('name', $value);
    }

    public function getLabel() {
        return $this->label;
    }

    public function setLabel($value) {
        $this->label = $value;
        $this->setAttrib('placeholder', $value);
    }

    public function getValue() {
		return $this->value;
	}

	public function setValue($value) {
		$this->value = $value;
	}

    public function setHelp($text) {
        $this->help = $text;
    }

    public function getHelp(){
        return $this->help;
    }

    public function getErrors() {
        return $this->errors;
    }

    public function addError($error) {
        $this->errors[] = $error;
    }

    public function getAttrib($name, $default=false){
        return array_key_exists($name,$this->attribs) ? $this->attribs[$name] : $default;
    }
    public function getAttribs(){
        return $this->attribs;
    }

    public function setAttrib($name, $value){
        $this->attribs{$name}= $value;
    }

    public function setAttribs($attribs){
        if (is_array($attribs)) {
            foreach ($attribs as $key=>$attrib){
                $this->setAttrib($key,$attrib);
            }
        }
    }

    public function setRequire($bool) {
        $this->isRequire = $bool;
        if ($bool) {
            $this->addValidator(new Engine_Form_Validator_NotEmpty());
        }
    }

    public function addValidator (Engine_Form_Validator_Abstract $validator) {
        $this->validators[] = $validator;
    }

    public function isValid($context = null) {
        $result = true;
        if (is_array($this->validators) && !empty($this->validators)) {
            foreach ($this->validators as $validator) {
                if ($context) $validator->setContext($context);

                $isValid = $validator->validate($this->value);
                if (!$isValid)
                {
                    $this->addError($validator->getError());
                    $result = false;
                }
            }
        }
        return $result;
    }

    public function renderLabel() {
        $result = null;
        if ($this->label && $this->showLabel) {
            $result =  "<label>" . $this->label . "</label>";
        }
        return $result;
    }

    public function renderHelp() {
        $result = null;
        if ($this->help) {
            $result =  "<span class=\"help-block\">" . $this->help . "</span>";
        }
        return $result;
    }


    public function render () {
        $result = '';
        $result .= $this->renderLabel();
        $result .= $this->compile();

        $errors = $this->getErrors();
        if (!empty($errors)) {
            $result .= Engine_View_Helper_Bootstrap::error($errors);
        }
        return $result;
    }

    public function renderAttribs() {
        $attribs = $this->getAttribs();
        $result = '';
        if (is_array($attribs) && !empty($attribs)) {
            foreach ($attribs as $key=>$value)
                $result .= ' ' . $key . ' = "' . $value . '"';
        }
        return $result;
    }


}